/**
 * This provides a preferences page for managing pipelines in Funf Manager. 
 * 
 * It is targeted at Android API Level 8. To allow a large coverage. 
 */
package com.pels.funf.ext.notifications;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.widget.ToggleButton;
import edu.mit.media.funf.FunfManager;

public class Settings extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	private static final String KILL_SERVICE = "kill_service";

	private Set<String> disabledPipelines;
	private Set<String> allPipelines;

	private FunfManager funfManager;
	private ServiceConnection funfManagerConn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			funfManager = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			funfManager = ((FunfManager.LocalBinder) service).getManager();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String data = getIntent().getExtras().getString(
				FunfManagerWithNotifications.DISABLED_PIPELINES);
		disabledPipelines = new HashSet<String>(Arrays.asList(data.split(",")));
		System.out.println("Disabled Pipelines: " + disabledPipelines);

		data = getIntent().getExtras().getString(
				FunfManagerWithNotifications.ENABLED_PIPELINES);
		allPipelines = new HashSet<String>(Arrays.asList(data.split(",")));
		allPipelines.addAll(disabledPipelines);
		System.out.println("All Pipelines: " + allPipelines);

		setPreferenceScreen(createPreferenceHierarchy()); 
		
		bindService(new Intent(this, FunfManagerWithNotifications.class),
				funfManagerConn, BIND_AUTO_CREATE);
	}


	private PreferenceScreen createPreferenceHierarchy() {
		// Root
		PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
				this);

		// Preference category for service
		PreferenceCategory servicePrefCat = new PreferenceCategory(this);
		servicePrefCat.setTitle("Funf Manager Service");
		root.addPreference(servicePrefCat);

		// stop service preference
		Preference killPref = new Preference(this);
		killPref.setKey(KILL_SERVICE);
		killPref.setTitle("Stop Service");
		killPref.setSummary("Click to stop Funf Manager Service");
		killPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				// TODO Find out why the code below isn't having the effect it
				// should.
				// stopService(new Intent(getApplicationContext(),
				// FunfManagerWithNotifications.class));
				funfManager.stopSelf();
				return false;
			}
		});
		servicePrefCat.addPreference(killPref);

		// Preference category for Pipelines
		PreferenceCategory pipelinesPrefCat = new PreferenceCategory(this);
		pipelinesPrefCat.setTitle("Pipelines");
		root.addPreference(pipelinesPrefCat);

		for (String pipeline : allPipelines) {
			if (!pipeline.equalsIgnoreCase("")) {
				CheckBoxPreference p = getPipelineTogglePreference(pipeline);
				p.setChecked(!disabledPipelines.contains(pipeline));
				pipelinesPrefCat.addPreference(p);
			}
		}
		return root;
	}

	private CheckBoxPreference getPipelineTogglePreference(String pipeline) {
		CheckBoxPreference togglePref = new CheckBoxPreference(this);
		togglePref.setKey("pipeline_" + pipeline);
		togglePref.setTitle(pipeline);
		System.out.println("Default value: "
				+ !disabledPipelines.contains(pipeline));
		return togglePref;
	}

	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (!key.equals(KILL_SERVICE)) {
			CheckBoxPreference selectedPref = (CheckBoxPreference) findPreference(key);
			String pipelineName = key.split("_")[1];
			if (selectedPref.isChecked()) {
				funfManager.enablePipeline(pipelineName);
			} else {
				funfManager.disablePipeline(pipelineName);
			}
		}

	}

}
